package com.wbs.shop.nuby.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbs.shop.nuby.R;


public class TitleView extends RelativeLayout {
    private View mNavigationBack;
    private TextView mNavigationTitle;
    private boolean mShowBack;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        int mTextSize = typedArray.getDimensionPixelOffset(R.styleable.TitleView_text_size, 17);
        mShowBack = typedArray.getBoolean(R.styleable.TitleView_show_Back, true);
        int mTextColor = typedArray.getColor(R.styleable.TitleView_text_color, ContextCompat.getColor(getContext(), R.color.white));
        String mTitle = typedArray.getString(R.styleable.TitleView_title);
        typedArray.recycle();
        inflate(context, R.layout.view_navigation, this);
        mNavigationBack = findViewById(R.id.tv_navigation_back);
        mNavigationTitle = (TextView) findViewById(R.id.tv_navigation_title);

        if (!TextUtils.isEmpty(mTitle)) {
            mNavigationTitle.setText(mTitle);
        }
        mNavigationTitle.setTextColor(mTextColor);
        mNavigationTitle.setTextSize(mTextSize);
        if (mShowBack) {
            mNavigationBack.setOnClickListener(v -> ((Activity) getContext()).finish());
        } else {
            mNavigationBack.setVisibility(GONE);
        }
    }

    public void setNavigationBackListener(OnClickListener listener) {
        if (mShowBack)
            mNavigationBack.setOnClickListener(listener);
    }

    public void setTitle(int titleRes) {
        setTitle(getResources().getString(titleRes));
    }

    public void setTitle(String title) {
        if (mNavigationTitle != null) {
            mNavigationTitle.setText(title);
        }
    }
}
